﻿using System;
using System.Data;
using System.Drawing;
using System.Net.Mail;
using System.Windows.Forms;

namespace wowSteadNotification
{
    public partial class wsn_FE : Form
    {
        private wsn_BL wsnForum;
        private wowSteadNotification.Properties.Settings settings;

        public wsn_FE()
        {
            InitializeComponent();
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            label1.Text += System.Reflection.Assembly.GetAssembly(this.GetType()).GetName().Version.ToString();
            settings = new wowSteadNotification.Properties.Settings();
            settings = Properties.Settings.Default;
            textBox1.Text = settings.WoWSteadURL;
            richTextBox1.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset0 Courier New;}}" + 
                @"{\viewkind4\uc1\pard\sa200\sl240\slmult1\b\f0\fs22 WowStead-Notification\line *********************\fs24\par" +
                @"\b0\fs20 Usage: wowSteadNotification.exe \i [silent]\line\pard\li720\sa200\sl240\slmult1\i0 --> Use parameter 'silent' for automated execution w/o frontend.\par" + 
                @"\pard\sa200\sl240\slmult1 For configuration take a close look at the XML based and self-explaining config file.\par}";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            wsnForum = new wsn_BL(textBox1.Text,
                settings.KeksCharID, 
                settings.KeksPassHash, 
                settings.KeksUserID, 
                settings.KeksLastVisit);
            if (wsnForum.ForumPosts != null)
            {
                button1.ForeColor = Color.DarkGreen;
                dataGridView1.DataSource = wsnForum.ForumPosts;
                if (wsnForum.ForumPosts.Rows.Count > 0 && checkBox1.CheckState == CheckState.Checked)
                    wsnForum.mailNotifications(settings.MailSubject,
                        settings.MailBody, settings.MailTrailer,
                        settings.EMaiFrom, settings.EMailsNotification,
                        settings.EMailServerSMTP);
                if (settings.RSSFlag)
                    wsnForum.createRSS(settings.RSSPath, settings.EMaiFrom);
            }
            else
            {
                button1.ForeColor = Color.Firebrick;
            }
            if (wsnForum.Errors != null && wsnForum.Errors.Count > 0)
            {
                string errormessages = "";
                foreach (Exception error in wsnForum.Errors)
                    errormessages += error.Message + "\n******************\n";
                MessageBox.Show(errormessages);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://t-roschinsky.spaces.live.com");
            linkLabel1.LinkVisited = true;
        }
    }
}
